
<div class="layui-card">
	<div class="layui-card-header">
		<h2 class="header-title">商家配置信息</h2>
		<span class="layui-breadcrumb pull-right"> <a href="#!home">首页</a>
			<a><cite>商家配置信息</cite></a>
		</span>
	</div>
	<div class="businessconfig_content_text"  id="businessconfig_content_text"
		style="width: 60%; margin-left: 20%;"></div>
</div>
<script type="text/html" id="businessconfig">
    <form id="businessconfig-form"  lay-filter="businessconfig-form" class="layui-form model-form" >
        {{#  layui.each(d, function(index, item){   }}
        <div class="layui-form-item">
            <label class="layui-form-label"  >{{item.configName}}</label>
            {{# if(item.valueType=='file'){        }}
            <input name="{{item.configKey}}" id="{{item.configKey}}" type="hidden" value="{{item.configValue?item.configValue:item.defaultValue }}"  lay-verify="required" required />
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="{{item.configKey}}Key">上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="{{item.configKey}}showImg" src="{{ item.configValue?item.configValue:item.defaultValue}}"  style='width:120px;height:120px;margin-left:110px;' >
                    <p id="{{item.configKey}}UploadText"></p>
                </div>
            </div>
            {{# }else{ }}
            <div class="layui-input-block">
                <input type="{{item.valueType}}"      {{# if(item.valueType=='number'){        }} min="0" step="0.01"  {{# } }} class="layui-input"  lay-verify="required" required    name="{{item.configKey}}" placeholder="{{item.configName}}" value="{{# if(item.configValue!=undefined&&item.configValue!=null){}}{{item.configValue}}{{# }else{ }}{{item.defaultValue}}{{# } }}">
            </div>
            {{# } }}
        </div>
        {{#  }); }}
        <div class="layui-form-item model-form-footer">
            <button class="layui-btn" lay-filter="businessconfig-form-submit" lay-submit style="margin-right:48%;margin-bottom:50px;" >提交</button>
        </div>
    </form>


 
</script>
<script type="text/html" id='uploadfilescript'>
    {{#  layui.each(d, function(index, item){   }}
         {{# if(item.valueType=='file'){        }}
             var   upload{{item.configKey}}= upload.render({
             elem: '#{{item.configKey}}Key'
             ,accept: "images"
             ,url: config.base_server+'file/simditorUpVedioFile'  
             ,before: function(obj){
                console.log(obj);
                 obj.preview(function(index, file, result){
                   $('#{{item.configKey}}showImg').attr('src', result);
                 });

             }
             ,done: function(res){	
                 layer.closeAll('loading');
               if(res.code > 0){
                 return layer.msg('上传失败');
               }
               $("#{{item.configKey}}").val(res.data.file_path);
               $(".layui-upload-file").hide();
             }
             ,error: function(){
                 layer.closeAll('loading');
                 var uploadText = $('#{{item.configKey}}UploadText');
                 uploadText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs upload-reload">重试</a>');
                 uploadText.find('.upload-reload').on('click', function(){
                 upload{{item.configKey}}.upload();
               });
             }
           });
        {{# } }}
  {{#  }); }}

 </script>
     
<script>
	var upload, config  ;
    layui.use(['layer', 'admin', 'form', 'formSelects', 'upload','laytpl','element','config'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var formSelects = layui.formSelects;
        let laytpl = layui.laytpl;
        let element = layui.element;
        upload = layui.upload;
        config = layui.config;
	        

		layer.load(2);
		admin.req('business/businessconfig/list', {}, function(data) {
			layer.closeAll('loading');
			if (data.code == 0) {
				layer.msg(data.msg, {
					icon : 1,
					time : 500
				});
				$("#businessconfig_content_text").html("");
				laytpl($("#businessconfig")[0].innerHTML).render( data.data, function(html) { //模板加载
							$("#businessconfig_content_text").append(html);

							element.render('nav');
							laytpl($("#uploadfilescript")[0].innerHTML).render( data.data, function(jstext) { //模板 加载脚本

                                         setTimeout(()=>{eval(jstext);},300);
									});
						});

			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 500
				});
			}
			$(".layui-upload .layui-upload-file").remove();
		}, "get");

        // var uploadDEVICE_ADV = upload.render({
        //     elem: '#DEVICE_ADVKey'
        //     ,accept: "images"
        //     ,url: config.base_server+'file/simditorUpVedioFile' //上传接口
        //     ,before: function(obj){
        //         //预读本地文件示例，不支持ie8
        //         obj.preview(function(index, file, result){
        //             $('#DEVICE_ADVshowImg').attr('src', result); //图片链接（base64）
        //         });
        //         layer.load(2);
        //     }
        //     ,done: function(res){
        //         layer.closeAll('loading');
        //         //如果上传失败
        //         if(res.code > 0){
        //             return layer.msg('上传失败');
        //         }
        //         console.log(res)
        //         $("#DEVICE_ADV").val(res.data.file_path);
        //         $(".layui-upload-file").remove();
        //         //上传成功
        //     }
        //     ,error: function(){
        //         layer.closeAll('loading');
        //         //失败状态，并实现重传
        //         var uploadText = $('#DEVICE_ADVUploadText');
        //         uploadText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs upload-reload">重试</a>');
        //         uploadText.find('.upload-reload').on('click', function(){
        //             uploadDEVICE_ADV.upload();
        //         });
        //     }
        // });

		// 表单提交事件
		form.on('submit(businessconfig-form-submit)', function(data) {
			layer.load(2);
			admin.req('business/businessconfig/save', JSON.stringify(data.field), function(data) {
				layer.closeAll('loading');
				if (data.code == 0) {
					layer.msg("更新完成！", {
						icon : 1,
						time : 1000
					});
					//   admin.finishPopupCenter();
				} else {
					layer.msg(data.msg, {
						icon : 2,
						time : 500
					});
				}
				element.render('nav');
				form.render();
			}, "post");
			return false;
		});
	});
</script>